﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DQYT.HTDZ.BLL;
using System.Collections;
using System.Drawing;

namespace DQYT.HTDZ.Web.Views.SystemManage
{
    public partial class RoleSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            try
            {
                lblMsg.Text = "";
                BDMenuBLL menuBLL = new BDMenuBLL();
                lblPath.Text = menuBLL.GetTitlePath(Request["mnu_id"]);

                
                if (!IsPostBack)
                {
                    InitData();
                }
            }
            catch (Exception ex)
            {
                
            }
            #endregion
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            DataTable dt = new DataTable();
            DataTable dtTemp = new DataTable();
            BDMenuBLL dMenu = new BDMenuBLL();
            DataTable dtJSCD = new DataTable();
            BDRoleMenuBLL dJSCD = new BDRoleMenuBLL();
            dtJSCD = dJSCD.GetList(" JSE_ID=" + Request["id"].ToString()).Tables[0];
            this.ViewState["dtjscd"] = dtJSCD;

            dt = dMenu.GetList("MNU_ZT=1").Tables[0];
            dtTemp = dt.Clone();
            if (dt == null || dt.Rows.Count < 1)
            {
                dt.Rows.Add(dt.NewRow());
            }
            else
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string strPID = dt.Rows[i]["mnu_fid"].ToString();

                    if (strPID == "0" || strPID == "")
                    {
                        dtTemp.Rows.Add(dt.Rows[i].ItemArray);
                        string strID = dt.Rows[i]["mnu_id"].ToString();
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            string strPID1 = dt.Rows[j]["mnu_fid"].ToString();
                            if (strPID1 == strID)
                            {
                                dt.Rows[j]["MNU_MC"] = " |--" + dt.Rows[j]["MNU_MC"].ToString();
                                dtTemp.Rows.Add(dt.Rows[j].ItemArray);

                                //第三层
                                string strID2 = dt.Rows[j]["mnu_id"].ToString();
                                for (int x = 0; x < dt.Rows.Count; x++)
                                {
                                    string strPID2 = dt.Rows[x]["mnu_fid"].ToString();
                                    if (strPID2 == strID2)
                                    {
                                        dt.Rows[x]["MNU_MC"] = "  |--" + dt.Rows[x]["MNU_MC"].ToString();
                                        dtTemp.Rows.Add(dt.Rows[x].ItemArray);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            gvData.DataSource = dtTemp;
            gvData.DataKeyNames = new string[] { "MNU_ID", "MNU_FID" };
            gvData.Columns[0].Visible = false;
            gvData.DataBind();
        }

        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果没有数据则添加一行“没有数据”行
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string sRow = e.Row.Cells[2].Text.Trim().ToString().Replace("&nbsp;", "");

                    if (string.IsNullOrEmpty(sRow))
                    {
                        
                        return;
                    }
                    e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();

                    string strFID = gvData.DataKeys[e.Row.RowIndex]["MNU_FID"].ToString();
                    string strID = gvData.DataKeys[e.Row.RowIndex]["MNU_ID"].ToString();
                    if (strFID == "0" || strFID == "")
                    {
                        e.Row.Cells[3].Text = "父节点";
                        e.Row.Cells[3].ForeColor = Color.Red;
                        e.Row.Cells[2].ForeColor = Color.Red;
                    }
                    else
                    {
                        //e.Row.Cells[2].Text = "&nbsp;|--" + e.Row.Cells[2].Text;
                        e.Row.Cells[3].Text = "子节点";
                    }
                    bool isCZ = false;
                    DataTable dt = (DataTable)this.ViewState["dtjscd"];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (strID == dt.Rows[i]["MNU_ID"].ToString())
                        {
                            isCZ = true;
                            break;
                        }
                    }
                    if (isCZ)
                    {
                        CheckBox ck = e.Row.Cells[5].FindControl("ckFlag") as CheckBox;
                        ck.Checked = isCZ;
                    }

                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message.ToString());
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 保存权限
            try
            {
                lblMsg.Text = "";
                DataTable dtJSCD = new DataTable();
                BDRoleMenuBLL dJSCD = new BDRoleMenuBLL();
                ArrayList alSQL = new ArrayList();

                if (Request["id"] == null || Request["id"].ToString().Trim() == "")
                {
                    lblMsg.Text = "参数丢失";
                    return;
                }
                string strJSE_ID = Request["id"].ToString();
                int iMaxID = dJSCD.GetNextID();
                string strSQL = "DELETE tb_sys_role_menu WHERE jse_id=" + strJSE_ID;
                alSQL.Add(strSQL);
                for (int i = 0; i < gvData.Rows.Count; i++)
                {
                    CheckBox ck = gvData.Rows[i].Cells[5].FindControl("ckFlag") as CheckBox;
                    if (ck.Checked)
                    {
                        string strID = gvData.DataKeys[gvData.Rows[i].RowIndex]["MNU_ID"].ToString();
                        strSQL = "insert into tb_sys_role_menu(JSD_ID,mnu_id,jse_id) values(" + iMaxID.ToString() + "," + strID + "," + strJSE_ID + ")";
                        alSQL.Add(strSQL);
                        iMaxID++;
                    }
                }
                dJSCD.ExecuteSqlTran(alSQL);
                lblMsg.Text = "更新成功";
                Response.Redirect("RoleManage.aspx");
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message.ToString();
            }

            #endregion
        }
    }
}